package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.SealJurVo;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SealJurMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 个人印章授权Service业务层处理
 *
 * @author ess
 * @date 2021-11-03
 */
@Service
public class SealJurSignerServiceImpl implements ISealJurSignerService {
    @Autowired
    private SealJurMapper sealJurMapper;

    @Autowired
    private ISealJurSignerService sealJurSignerService;

    @Autowired
    private ISealInfoService sealInfoService;

    @Autowired
    private IBusinessSysService businessSysService;


    /**
     * 查询个人印章授权
     *
     * @param sealJurId 印章授权主键
     * @return 印章授权
     */
    @Override
    public SealJur selectSealJurSignerBySealJurId(String sealJurId) {
        SealJur jur = sealJurMapper.selectSealJurBySealJurId(sealJurId);
        if (jur != null) {

            SealJurVo sealJurVo = new SealJurVo();
//                印章名称
            if (StringUtils.isNotEmpty(jur.getSealId())) {
                SealInfo sealInfo = sealInfoService.selectSealInfoBySealInfoId(jur.getSealId());
                if (sealInfo != null) {
                    sealJurVo.setSealName(sealInfo.getSealName());
                } else {
                    sealJurVo.setSealName(Constants.ERR_NAME);
                }
            }

            if (StringUtils.isEmpty(jur.getIp())) {
                sealJurVo.setIpName(Constants.NOT_IMPOSE);
            }
            if (StringUtils.isEmpty(jur.getMac())) {
                sealJurVo.setMacName(Constants.NOT_IMPOSE);
            }
            if (StringUtils.isEmpty(jur.getFileTypeNum())) {
                sealJurVo.setFileTypeName(Constants.NOT_IMPOSE);
            }
            if (jur.getUseCount() == null || jur.getUseCount() == -1) {
                sealJurVo.setUseContName(Constants.NOT_IMPOSE);
            }
//                业务系统名称 如有多个
            if (StringUtils.isNotEmpty(jur.getBusinessSysIds())) {
                String businessSysNames = "";
                String[] splitBusinessSys = jur.getBusinessSysIds().split(",");
                for (String splitBusinessSy : splitBusinessSys) {
                    BusinessSys businessSys = businessSysService.selectBusinessSysByBSysId(splitBusinessSy);
                    businessSysNames += businessSys.getbName() + ",";
                }
                sealJurVo.setBusinessSysNames(businessSysNames.substring(0, businessSysNames.length() - 1));
            } else {
                sealJurVo.setBusinessSysNames(Constants.NOT_IMPOSE);
            }

            jur.setSealJurVo(sealJurVo);
        }
        return jur;
    }

    /**
     * 查询个人印章授权列表
     *
     * @param sealJur 印章授权
     * @return 印章授权
     */
    @Override
    public List<SealJur> selectSealJurSignerList(SealJur sealJur) {

        ArrayList<SealJur> sealJurArrayList = new ArrayList<>();

        //印章姓名
        if (StringUtils.isNotEmpty(sealJur.getSealName())) {
            List<SealInfo> sealInfos = sealInfoService.selectSealInfoBySealName(sealJur.getSealName());
            if (sealInfos != null && sealInfos.size() > 0) {
                for (SealInfo sealInfo : sealInfos) {
                    sealJur.setSealId(sealInfo.getSealInfoId());
                    List<SealJur> jurList = sealJurMapper.selectSealJurList(sealJur);
                    if (jurList != null && jurList.size() > 0) {
                        for (SealJur jur : jurList) {
                            sealJurArrayList.add(jur);
                        }
                    }

                }

            }

            return takeSealJurReData(sealJurArrayList);
        }
        return takeSealJurReData(sealJurMapper.selectSealJurList(sealJur));
    }

    @Transactional
    @Override
    public int updateSealSignerJur(SealJur sealJur) {
        return sealJurMapper.updateSealJur(sealJur);
    }

    /**
     * 批量删除个人印章授权
     *
     * @param sealJurIds 需要删除的印章授权主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSealJurBySealJurSignerIds(String[] sealJurIds) {
        return sealJurMapper.deleteSealJurBySealJurIds(sealJurIds);
    }


    /**
     * 删除个人印章授权信息
     *
     * @param sealJurId 印章授权主键
     * @return 结果
     */
    @Override
    public int deleteSealJurBySealJurSignerId(String sealJurId) {
        return sealJurMapper.deleteSealJurBySealJurId(sealJurId);
    }

    @Override
    public boolean checkDueTime(String dueTime, String sealJurId) {
        try {
//        判断跟当前时间
            Date date = new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dueTimeDate = sf.parse(dueTime);

            if (dueTimeDate.before(date)) {
                return false;
            }

            SealJur sealJur = sealJurSignerService.selectSealJurSignerBySealJurId(sealJurId);
            if (sealJur != null) {
                Date parseOldDueDate = sf.parse(sealJur.getDueTime());
                if (parseOldDueDate.before(date)) {
                    return false;
                }

                if (dueTimeDate.after(parseOldDueDate)) {
                    return false;
                } else {
                    return true;
                }

            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean checkUseCount(Long useCount, String sealJurId) {
        try {
            SealJur sealJur = sealJurSignerService.selectSealJurSignerBySealJurId(sealJurId);
            if (sealJur != null) {


                if (sealJur.getUseCount() == null || sealJur.getUseCount() == -1) {
                    return true;
                } else {
                    if (sealJur.getUseCount() == 0) {
                        return false;
                    }

                    if (useCount >= sealJur.getUseCount()) {
                        return false;
                    } else {
                        return true;
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    private List<SealJur> takeSealJurReData(List<SealJur> sealJurs){
        if (sealJurs != null && sealJurs.size() > 0) {
            for (SealJur jur : sealJurs) {
                SealJurVo sealJurVo = new SealJurVo();
//                印章名称
                if (StringUtils.isNotEmpty(jur.getSealId())) {
                    SealInfo sealInfo = sealInfoService.selectSealInfoBySealInfoId(jur.getSealId());
                    if (sealInfo != null) {
                        sealJurVo.setSealName(sealInfo.getSealName());
                    } else {
                        sealJurVo.setSealName(Constants.ERR_NAME);
                    }
                }

                if (StringUtils.isEmpty(jur.getIp())) {
                    sealJurVo.setIpName(Constants.NOT_IMPOSE);
                }
                if (StringUtils.isEmpty(jur.getMac())) {
                    sealJurVo.setMacName(Constants.NOT_IMPOSE);
                }
                if (StringUtils.isEmpty(jur.getFileTypeNum())) {
                    sealJurVo.setFileTypeName(Constants.NOT_IMPOSE);
                }
                if (jur.getUseCount() == null || jur.getUseCount() == -1) {
                    sealJurVo.setUseContName(Constants.NOT_IMPOSE);
                }
//                业务系统名称 如有多个
                if (StringUtils.isNotEmpty(jur.getBusinessSysIds())) {
                    String businessSysNames = "";
                    String[] splitBusinessSys = jur.getBusinessSysIds().split(",");
                    for (String splitBusinessSy : splitBusinessSys) {
                        BusinessSys businessSys = businessSysService.selectBusinessSysByBSysId(splitBusinessSy);
                        businessSysNames += businessSys.getbName() + ",";
                    }
                    sealJurVo.setBusinessSysNames(businessSysNames.substring(0, businessSysNames.length() - 1));
                } else {
                    sealJurVo.setBusinessSysNames(Constants.NOT_IMPOSE);
                }

                jur.setSealJurVo(sealJurVo);
            }
        }

        return sealJurs;

    }
}
